﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;

namespace EncryptionV1V2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            String[] files = GetFiles();
            string outputPath = textBox1.Text;

            if (files.Length != 0 && textBox1.Text != "")
            {
                if (MessageBox.Show("Decrypt V1 and Encrypt to V2?", "Confirm Encrypt", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    label3.Text = "Processing...";
                    
                    foreach (string filename in files)
                    {
                        string[] array = filename.Split('\\');
                        string outfilename = array[array.Length - 1];
                        string V1outfilepath = outputPath + "\\" + outfilename;

                        decryptV1(filename, V1outfilepath);

                        string V2OutfileName = outputPath + "\\" + Path.GetFileName(filename).Replace("pdf", "bpaper");
                        encryptV2(V1outfilepath, V2OutfileName);
                    }

                    MessageBox.Show("Task Completed Successfully!");
                    textBox1.Text = "";
                    label3.Text = "";
                }
                else
                {
                    MessageBox.Show("Cancelled!");
                }
            }
            else
            {
                MessageBox.Show("Incorrect path!");
            }
        }

        public string[] GetFiles()
        {
            string[] fileNames;

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Multiselect = true;
            openFileDialog1.CheckFileExists = false;
            openFileDialog1.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = false;

            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                return fileNames = openFileDialog1.FileNames;
            }
            else
            {
                return null;
            }
        }

        public void decryptV1(string inputFilePath, string outputFilePath)
        {
            string password = "myKey123";

            try
            {
                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);

                FileStream fsCrypt = new FileStream(inputFilePath, FileMode.Open);

                RijndaelManaged RMCrypto = new RijndaelManaged();

                CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);
                FileStream fsOut = new FileStream(outputFilePath, FileMode.Create);


                byte[] buffer = new byte[1024 * 1024 * 2];
                int count = 0;
                int i = 0;

                while ((count = cs.Read(buffer, 0, 1024 * 1024 * 2)) > 0)
                {
                    fsOut.Write(buffer, 0, count);
                    //cs.Write(buffer, 0, count);
                    i = i + count;
                }

                cs.Close();
                fsCrypt.Close();
                fsOut.Close();

            }
            catch (IOException e)
            {
                MessageBox.Show("V1 Decryption Stopped due to IO Exception. Exception Details: " + e.Message + e.InnerException);
            }
            catch (Exception e)
            {
                MessageBox.Show("V1 Decryption Stopped due to an Exception. Exception Details: " + e.Message + e.InnerException);
            }
        }

        public void encryptV2(string inputFilePath, string outputFilePath)
        {
            if (System.IO.File.Exists(inputFilePath)) //check is file is there
            {
                try
                {
                    string password = @"myKey123"; // Your Key Here
                    UnicodeEncoding UE = new UnicodeEncoding();
                    byte[] key = UE.GetBytes(password);

                    string cryptFile = outputFilePath;
                    FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

                    RijndaelManaged RMCrypto = new RijndaelManaged();

                    CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);

                    FileStream fsIn = new FileStream(inputFilePath, FileMode.Open);

                    int data;
                    while ((data = fsIn.ReadByte()) != -1)
                        cs.WriteByte((byte)data);

                    fsIn.Close();
                    cs.Close();
                    fsCrypt.Close();
                    System.IO.File.Delete(inputFilePath);

                }
                catch (IOException e)
                {
                    MessageBox.Show("V2 Encryption Stopped due to IO Exception. Exception Details: "+ e.Message + e.InnerException);
                }
                catch (Exception e)
                {
                    MessageBox.Show("V2 Encryption Stopped due to an Exception. Exception Details: "+ e.Message + e.InnerException);
                }
            }
        }

       
    }
}
